<?php

require_once ($GLOBALS['fileroot'] . "/library/classes/Controller.class.php");
require_once ($GLOBALS['fileroot'] . "/library/forms.inc");
require_once ($GLOBALS['fileroot'] . "/library/patient.inc");
require_once("FormVitals.class.php");

class C_FormVitals extends Controller {

    var $template_dir;

    function C_FormVitals($template_mod = "general") {
        parent::Controller();
        $returnurl = $GLOBALS['concurrent_layout'] ? 'encounter_top.php' : 'patient_encounter.php';
        $this->template_mod = $template_mod;
        $this->template_dir = dirname(__FILE__) . "/templates/vitals/";
        $this->assign("FORM_ACTION", $GLOBALS['web_root']);
        $this->assign("DONT_SAVE_LINK", $GLOBALS['webroot'] . "/interface/patient_file/encounter/$returnurl");
        $this->assign("STYLE", $GLOBALS['style']);

        // send the unit selection
        $this->assign("units_of_measurement", $GLOBALS['units_of_measurement']);
    }

    function default_action_old() {
        //$vitals = array();
        //array_push($vitals, new FormVitals());
        $vitals = new FormVitals();
        $this->assign("vitals", $vitals);
        $this->assign("results", $results);
        return $this->fetch($this->template_dir . $this->template_mod . "_new.html");
    }

    function default_action($form_id) {
        if (is_numeric($form_id)) {
            $vitals = new FormVitals($form_id);
        } else {
            $vitals = new FormVitals();
        }

        $dbconn = $GLOBALS['adodb']['db'];
        //Combined query for retrieval of vital information which is not deleted
//        $sql = "SELECT fv.*, fe.date AS encdate " .
//                "FROM form_vitals AS fv, forms AS f, form_encounter AS fe WHERE " .
//                "fv.id != $form_id and fv.pid = " . $GLOBALS['pid'] . " AND " .
//                "f.formdir = 'vitals' AND f.deleted == 0 AND f.form_id = fv.id AND " .
//                "fe.pid = f.pid AND fe.encounter = f.encounter " .
//                "ORDER BY encdate DESC, fv.date DESC";
//        if ($_SESSION['patient_portal_sac'] == 1)
        
        $source_entry = "";
        
        if($_REQUEST['view'] == 0)
            $source_entry = " and source_entry='0' ";
        else if($_REQUEST['view'] == 1)
            $source_entry = " and source_entry='1' ";
        $sql = "SELECT fv. *
                FROM form_vitals AS fv, forms AS f
                WHERE fv.id !=0
                AND fv.pid =" . $GLOBALS['pid'] . "
                AND f.formdir = 'vitals'
                AND f.deleted =0
                AND f.form_id = fv.id ". $source_entry."
                ORDER BY fv.date DESC";
//        echo $sql;
        $result = $dbconn->Execute($sql);

        // get the patient's current age
        $patient_data = getPatientData($GLOBALS['pid']);
        $patient_age = getPatientAge($patient_data['DOB']);
        $this->assign("patient_age", $patient_age);
          
        $i = 1;
        while ($result && !$result->EOF) {
            $results[$i]['id'] = $result->fields['id'];
            $results[$i]['encdate'] = substr($result->fields['encdate'], 0, 10);
            $results[$i]['date'] = $result->fields['date'];
            $results[$i]['activity'] = $result->fields['activity'];
            $results[$i]['bps'] = $result->fields['bps'];
            $results[$i]['bpd'] = $result->fields['bpd'];
            $results[$i]['weight'] = $result->fields['weight'];
            $results[$i]['height'] = $result->fields['height'];
            $results[$i]['temperature'] = $result->fields['temperature'];
            $results[$i]['temp_method'] = $result->fields['temp_method'];
            $results[$i]['pulse'] = $result->fields['pulse'];
            $results[$i]['respiration'] = $result->fields['respiration'];
            $results[$i]['BMI'] = $result->fields['BMI'];
            $results[$i]['BMI_status'] = $result->fields['BMI_status'];
            $results[$i]['note'] = $result->fields['note'];
            $results[$i]['waist_circ'] = $result->fields['waist_circ'];
            $results[$i]['head_circ'] = $result->fields['head_circ'];
            $results[$i]['bodyfatpercent'] = ($result->fields['bodyfatpercent'] == 0) ? '' : $result->fields['bodyfatpercent'];
            $results[$i]['bodyfatrange'] = ($result->fields['bodyfatrange'] == 0) ? '' : $result->fields['bodyfatrange'];
            $results[$i]['bodywaterpercent'] = ($result->fields['bodywaterpercent'] == 0) ? '' : $result->fields['bodywaterpercent'];
            $results[$i]['bodywatermass'] =($result->fields['bodywatermass'] == 0) ? '' : $result->fields['bodywatermass'];
            $results[$i]['visceralfatrating'] = ($result->fields['visceralfatrating'] == 0) ? '' : $result->fields['visceralfatrating'];
            $results[$i]['dailycalorieintake'] = ($result->fields['dailycalorieintake'] == 0) ? '' : $result->fields['dailycalorieintake'];
            $results[$i]['metabolicage'] = ($result->fields['metabolicage'] == 0) ? '' : $result->fields['metabolicage'];
            $results[$i]['musclemass'] = ($result->fields['musclemass'] == 0) ? '' : $result->fields['musclemass'];
            $results[$i]['physiquerating'] = ($result->fields['physiquerating'] == 0) ? '' : $result->fields['physiquerating'];
            $results[$i]['bonemass'] = ($result->fields['bonemass'] == 0) ? '' : $result->fields['bonemass'];
            $results[$i]['fatmass'] = ($result->fields['fatmass'] == 0) ? '' : $result->fields['fatmass'];
            $results[$i]['fatfreemass'] = ($result->fields['fatfreemass'] == 0) ? '' : $result->fields['fatfreemass'];
            $results[$i]['basalmetabolicrate'] = ($result->fields['basalmetabolicrate'] == 0) ? '' : $result->fields['basalmetabolicrate'];
            $results[$i]['source_entry'] = $result->fields['source_entry'];
            $results[$i++]['oxygen_saturation'] = ($result->fields['oxygen_saturation'] == 0) ? '' : $result->fields['oxygen_saturation'];

            $result->MoveNext();
        }

        $this->assign("vitals", $vitals);
        $this->assign("results", $results);
        $this->assign("webroot", $webroot);
        if(isset($_REQUEST['medico'])){
            $this->assign("medico", $_REQUEST['medico']);
            $this->assign("medicoIni", "?medico=1");
        }
        
        $this->assign("VIEW", true);

        if ($form_id === 'new')
            return $this->fetch($this->template_dir . $this->template_mod . "_add_new.html");
        if ($_SESSION['patient_portal_sac'] == 1 || isset($_REQUEST['medico'])) {
            return $this->fetch($this->template_dir . $this->template_mod . "_new_sac.html");
        } else {
            return $this->fetch($this->template_dir . $this->template_mod . "_new.html");
        }

        //return $this->fetch($this->template_dir . $this->template_mod . "_new.html");
    }

    function default_action_process() {
        if ($_POST['process'] != "true")
            return;
           
        /*echo "<pre>";
        print_r($_POST);
        echo "</pre>";*/
        $date_tmp = str_replace('/', '-',   $_POST["date"]);
        $date= date('Y-m-d', strtotime($date_tmp));
        $_POST["date"] = $date;        
        
        $weight = $_POST["weight"];
        $height = $_POST["height"];
        if ($weight > 0 && $height > 0) {
            $_POST["BMI"] = ($weight / $height / $height) * 703;
        }
        if ($_POST["BMI"] > 42)
            $_POST["BMI_status"] = 'Obesity III';
        elseif ($_POST["BMI"] > 34)
            $_POST["BMI_status"] = 'Obesity II';
        elseif ($_POST["BMI"] > 30)
            $_POST["BMI_status"] = 'Obesity I';
        elseif ($_POST["BMI"] > 27)
            $_POST["BMI_status"] = 'Overweight';
        elseif ($_POST["BMI"] > 25)
            $_POST["BMI_status"] = 'Normal BL';
        elseif ($_POST["BMI"] > 18.5)
            $_POST["BMI_status"] = 'Normal';
        elseif ($_POST["BMI"] > 10)
            $_POST["BMI_status"] = 'Underweight';
        $temperature = $_POST["temperature"];
        if ($temperature == '0' || $temperature == '') {
            $_POST["temp_method"] = "";
        }
        if(isset($_SESSION['authUserID']))
            $_POST["provider_id"] = $_SESSION['authUserID'];
        else
            $_POST["provider_id"] = NULL;

        $this->vitals = new FormVitals($_POST['id']);

        parent::populate_object($this->vitals);

        $this->vitals->persist();
        if ($GLOBALS['encounter'] < 1) {
            $GLOBALS['encounter'] = date("Ymd");
        }
        if (empty($_POST['id'])) {
            addForm($GLOBALS['encounter'], "Vitals", $this->vitals->id, "vitals", $GLOBALS['pid'], $_SESSION['userauthorized']);
            $_POST['process'] = "";
        }
        return;
    }

}

?>
